<?php

namespace App\Http\Controllers\Web;

use App\Models\Orders;
use App\Models\Company;
use App\Models\Website;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use App\Models\Year;

class CertificationController extends Controller {



    public function home() {
        //dd($_SERVER);
        return view('Home.home');
    }

    public function manage() {
        return view('web.cert.manage');
    }

    public function order() {
        //需要判断是否登录
        $uid = Auth::user()->id; // 获取的是用户id
        $phone = Auth::user()->telphone; //获取的是用户手机号码
        $order = array(); //定义空数组
        $comp = '';
        $comp2 = '';
        if ($uid) {
            $comp = Orders::where(['uid' => $uid])->first(); //根据uid 查询
        }
        if ($phone) {
            $comp2 = Orders::where(['phone' => $phone])->first(); //根据uid 查询
        }
        if ($comp) {
            $order = Orders::where(['uid' => $uid])->get(); //根据uid 查询
            foreach ($order as $key => $val) {
                if ($val['order_type'] == 1) { //企业认证  conpany
                    $post = Company::find($val['relation_id']);

                    $order[$key]['year'] = $post['year'];

                    $order[$key]['contacts_name'] = $post['contacts_name'];
                    $order[$key]['contacts_emal'] = $post['contacts_emal'];
                    $order[$key]['contacts_phone'] = $post['contacts_phone'];
                } else if ($val['order_type'] == 0) { // 官网认证 website
                    $website = Website::find($val['relation_id']);


                    $order[$key]['year'] = $website['year'];

                    $order[$key]['year'] = $website['year'];

                    $order[$key]['contacts_name'] = $website['contacts_name'];
                    $order[$key]['contacts_emal'] = $website['contacts_emal'];
                    $order[$key]['contacts_phone'] = $website['contacts_phone'];
                }
            }
        } else if ($comp2 != '') {
            $order = Orders::where(['phone' => $phone])->get(); // 查询当前登录用户的信息
            // dd($order);
            foreach ($order as $key => $val) {
                if ($val['order_type'] == 1) { //企业认证  conpany
                    $post = Company::find($val['relation_id']);


                    $order[$key]['year'] = $post['year'];

                    $order[$key]['contacts_name'] = $post['contacts_name'];
                    $order[$key]['contacts_emal'] = $post['contacts_emal'];
                    $order[$key]['contacts_phone'] = $post['contacts_phone'];
                } else if ($val['order_type'] == 0) { // 官网认证 website
                    $website = Website::find($val['relation_id']);
                    $order[$key]['year'] = $website['year'];
                    $order[$key]['contacts_name'] = $website['contacts_name'];
                    $order[$key]['contacts_emal'] = $website['contacts_emal'];
                    $order[$key]['contacts_phone'] = $website['contacts_phone'];
                }
            }
        }
        return view('web.cert.order', ['order' => $order]);
    }

    public function materialgw(Request $request) {
        $order_id = $request->all('order_id'); //订单id
        // 判断是官网认证 执行第一个form  判断是实名认证执行第二个form
        return view('web.cert.material',['order_id'=>$order_id]);
    }

    //ajax 提交数据 gw
    public function ajaxmaterialgw(Request $request) {
        if ($request->ajax()) {
            $fullValue = $request->all(); //表单提交的所有内容       
            $certificate = $request->file('certificate');
            $certfull = $certificate->storeAs('/site', date("Y-m-d") . "/" . time() . $certificate->getClientOriginalExtension());
            $corporation = $request->file('business_corporation');
            $corpfull = $corporation->storeAs('/site', date("Y-m-d") . "/" . time() . $corporation->getClientOriginalExtension());
            $order_id = Orders::where(['id'=>$fullValue['order_id']])->first();
            $website = Website::where(['id' => $order_id['relation_id']])->first();
            //买了3年，赠送3年 = 6；
            $website->web_name = $fullValue['web_name'];
            $website->web_url = $fullValue['web_url'];
            $website->certificate = $certfull; //域名证书
            $website->icp_record_number = $fullValue['icp_record_number'];
            $website->telecom_value = $fullValue['telecom_value'];
            $website->website_operation = $fullValue['website_operation'];
            $website->company_name = $fullValue['company_name'];
            $website->company_address = $fullValue['company_address'];
            $website->legal_representative = $fullValue['legal_representative'];
            $website->business_scope = $fullValue['business_scope'];
            $website->business_corporation = $corpfull; //公司营业执照
            $website->id_card = $fullValue['id_card'];            
            $result = $website->save();  //保存
            if ($result) {
                //更新order 里面的order_status复审资料提交待复审 4
                $data = Orders::where(['id' => $fullValue['order_id'],'order_type'=>0])->first();
                $data->order_status = 4;
                if ($data->save()) {//保存
                    echo 1;
                    exit;
                } else {
                    echo 0;
                    exit;
                }
            } else {
                echo 0;
                exit;
            }
        }
    }

    //ajax 提交数据 sm
    public function ajaxmaterialsm(Request $request) {
        if ($request->ajax()) {
            $fullValue = $request->all(); //表单提交的所有内容
            $file = $request->file('business_license');
//               $file->getClientOriginalName().'.' // 文件名称
            $fullImg = $file->storeAs('/site', date("Y-m-d") . "/" . time() . $file->getClientOriginalExtension());
            
            $order_id = Orders::where(['id'=>$fullValue['order_id']])->first();
            $company = Company::where(['id' => $order_id['relation_id']])->first();
            $company->registration_number = $fullValue['registration_number'];
            $company->unified_code = $fullValue['unified_code'];
            $company->company_registered_address = $fullValue['company_registered_address'];
            $company->legal_representative = $fullValue['legal_representative'];
            $company->registered_capital = $fullValue['registered_capital'];
            $company->company_registration_time = strtotime($fullValue['company_registration_time']);
            $company->operation_period_start = strtotime($fullValue['operation_period_start']);
            $company->operation_period_end = strtotime($fullValue['operation_period_end']);
            $company->business_scope = $fullValue['business_scope'];
            $company->registration_authority = $fullValue['registration_authority'];
            $company->id_card = $fullValue['id_card'];             
            $company->business_license = $fullImg;
            $result = $company->save();  //保存            
            if ($result) {
                //更新order 里面的order_status复审资料提交待复审 4
                $data = Orders::where(['id' => $fullValue['order_id'],'order_type'=>1])->first();
                $data->order_status = 4;
                if ($data->save()) {//保存
                    echo 1;
                    exit;
                } else {
                    echo 0;
                    exit;
                }
            } else {
                echo 0;
                exit;
            }
        }
    }

    public function materialsm(Request $request) {
        $order_id = $request->all('order_id'); //订单id
        // 判断是官网认证 执行第一个form  判断是实名认证执行第二个form
        return view('web.cert.certification',['order_id'=>$order_id]);
    }

}
